var page=1;
$(function(){
    getMenu();
    //加载更多
    $(".moreLi").click(function(){
        if($(this).hasClass("moreLis")){
            page++;
            getGoods(page);
        }
    })
});
/*查询菜单*/
function getMenu(){
    ym.ajax({
        url:'/shop/cat/getCat',
        success:function(data){
            var $slider = $('#demo-slider-0');
            if(data.res ==1){
                var html="";
                if(data.obj!=null&&data.obj.length>0){
                    for(var i=0;i<data.obj.length;i++){
                        var l=data.obj[i];
                       html+='<span class="titleLi" codeid="'+l.id+'">'+l.text+'</span>'
                    }
                }
                $(".navDiv").append(html);
                $(".titleLi").click(function(){
                    $(this).addClass("activeNav").siblings("span").removeClass("activeNav");
                    var navId=$(this).attr("codeId");
                    if(navId==0){
                        getGoods(1);
                        $(".otherMenu").addClass("hide");
                        $(".homeDiv").removeClass("hide");
                    }else{
                        getOtherGoods(navId);
                        $(".homeDiv").addClass("hide");
                        $(".otherMenu").removeClass("hide");
                    }
                })
            }else{
                console.log("数据获取失败！")
            }
        }
    });
    getBroad();
}
/*图片轮播*/
function getBroad(){
    ym.ajax({
        url:'/shop/product/Carousel',
        success:function(data){
            if(data.res ==1){
                var html="";
                for(var i=0;i<data.obj.length;i++){
                   html+='<li onclick="jumpDetails('+data.obj[i].productId+')"><img src="'+data.obj[i].productPic+'" class="sowingImg" /></li>'
                }
                $("#slidesImg").append(html);
                $('.am-slider').flexslider({
                    playAfterPaused : 200,
                    animation : "slide",
                    direction : "horizontal",
                    slideshow : true,
                    animationLoop : true,
                    smoothHeight : true,
                    touch : true,
                    animationSpeed : 300
                });
            }else{console.log("数据获取失败！")}
        }
    });
    getGoods(1)
}
/*获取首页商品列表*/
function getGoods(page){
   $("#loadingModal").modal();
   if(page==1){
       $(".homeDiv .shopUls").html("");
   }
    ym.ajax({
        url:'/shop/product/getAll',
        param:{
            pageNumber:page,
        },
        success:function(data){
            var html="";
           if(data.obj!=null&&data.obj.length>0){
               for(var i=0;i<data.obj.length;i++){
                   var l=data.obj[i];
                   html+='<li class="goodsDiv" onclick="jumpDetails('+l.productId+')">'
                   html+='<span class="goodsImgDiv">'
                   html+='<img src="'+l.productPic+'" class="goodsImg wid100" />'
                   html+='</span>'
                   html+='<span class="goodsName">'+l.productName+'</span>'
                   html+='<span class="goodsMess">'
                   if(l.salesVolume==null||l.salesVolume==""){
                       l.salesVolume=5;
                   }
                   html+='<span class="goodsSale">已购'+l.salesVolume+'件</span>'
                   if(l.productType==2){
                       //积分
                       html+='<span class="goodsMoney">'+l.productPoint+'积分</span>'
                   }else{
                       //通用和直购
                       var nowPrice=(l.productPrice)*0.01;
                       nowPrice=nowPrice.toFixed(2);
                       html+='<span class="goodsMoney">￥ '+nowPrice+'</span>'
                   }
                   html+='</span></li>'
               }
           }else{
               if(page!=1){
                   $("#moreLi").removeClass("moreLis");
                   $("#moreLi").html("没有更多啦~");
               }else{
                   html+="<li style='width:100%;height:30px;line-height:30px;color:#ababab;margin-top:10px;'>暂无相关商品信息</li>"
               }
           }
            $(".homeDiv .shopUls").append(html);
            $("#loadingModal").modal('close');
        }
    });
}
/*获取剩余商品列表*/
function getOtherGoods(navId){
    $("#loadingModal").modal();
    $(".otherMenu .shopUls").html("");
    ym.ajax({
        url:'/shop/product/getByCatId',
        param:{
            catId:navId,
        },
        success:function(data){
            var html="";
            if(data.obj!=null&&data.obj.length>0){
                for(var i=0;i<data.obj.length;i++){
                    var l=data.obj[i];
                    html+='<li class="goodsDiv" onclick="jumpDetails('+l.productId+')">'
                    html+='<span class="goodsImgDiv">'
                    html+='<img src="'+l.productPic+'" class="goodsImg wid100" />'
                    html+='</span>'
                    html+='<span class="goodsName">'+l.productName+'</span>'
                    html+='<span class="goodsMess">'
                    if(l.salesVolume==null||l.salesVolume==""){
                        l.salesVolume=5;
                    }
                    html+='<span class="goodsSale">已购'+l.salesVolume+'件</span>'
                    if(l.productType==2){
                        //积分
                        html+='<span class="goodsMoney">'+l.productPoint+'积分</span>'
                    }else{
                        //通用和直购
                        var nowPrice=(l.productPrice)*0.01;
                        nowPrice=nowPrice.toFixed(2);
                        html+='<span class="goodsMoney">￥ '+nowPrice+'</span>'
                    }
                    html+='</span></li>'
                }
            }else{
               html+="<li style='width:100%;height:30px;line-height:30px;color:#ababab;margin-top:10px;'>暂无相关商品信息</li>"

            }
            $(".otherMenu .shopUls").append(html);
            $("#loadingModal").modal('close');
        }
    });
}
//跳转详情
function jumpDetails(goodsId){
    window.location.href="goodsDetails.html?goodsId="+goodsId;
}
/*底部导航条跳转*/
function pageJump(num){
    if(num==2){
        window.location.href="car.html";
    }else if(num==3){
        window.location.href="person.html";
    }
}
